package com.wythe.mall.activity;

import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wythe.mall.R;
import com.wythe.mall.base.BaseActivity;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.ClassifyBean;
import com.wythe.mall.model.UserBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.utils.SharePreferenceUtils;
import com.wythe.mall.utils.ToastUtil;
import com.wythe.mall.utils.UserManager;

import org.json.JSONException;
import org.json.JSONObject;

import top.yokey.base.base.BaseCountTime;
import top.yokey.base.base.BaseToast;
import top.yokey.base.util.TextUtil;

/**
 * Created by wythe on 2016/7/8.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText edUsername;
    private EditText edPassword;
    private CheckBox switchButton;
    private ImageView mBack;
    private ImageView loginPasswordDelete;
    private AppCompatTextView loginTextView, getTextView;
    private AppCompatEditText usernameEditText;
    private AppCompatEditText passwordEditText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
        initEven();
    }

    public void initView() {
        ((TextView) findViewById(R.id.titleText)).setText("登录");
        loginTextView = (AppCompatTextView) findViewById(R.id.loginTextView);
        getTextView = (AppCompatTextView) findViewById(R.id.getTextView);
        mBack = (ImageView) findViewById(R.id.title_left_button);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        loginTextView.setOnClickListener(this);
        getTextView.setOnClickListener(this);
        usernameEditText = (AppCompatEditText) findViewById(R.id.usernameEditText);
        passwordEditText = (AppCompatEditText) findViewById(R.id.passwordEditText);
    }

    public void initData() {

    }

    public void initEven() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.loginTextView:
                String username = usernameEditText.getText().toString();
                String password = passwordEditText.getText().toString();
                if (TextUtils.isEmpty(username)) {
                    ToastUtil.show(this, "请填写手机号码");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    ToastUtil.show(this, "请填写手机验证码");
                    return;
                }
                JSONObject param = new JSONObject();
                try {
                    param.put("code", password);
                    param.put("mobile", username);
                    param.put("type", "CODE");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                ApiOkHttp.doLogin(this, param, new OnRequestDataListener() {
                    @Override
                    public void requestSuccess(int code, JSONObject data) {
                        try {
                            JSONObject jsonObject = data.getJSONObject("data");
                            int code1 = data.getInt("code");
//                            UserBean bean = new Gson().fromJson(jsonObject.toString(), UserBean.class);
                            String token = jsonObject.getString("token");
                            String nickname = jsonObject.getString("nickname");
                            String avatarurl = jsonObject.getString("avatarurl");
                            if (code1 == 200 && token != null && !token.equals("null") && token.length() > 0) {
                                SharePreferenceUtils.putString(LoginActivity.this, "KEY_TOKEN", token);
                                SharePreferenceUtils.putString(LoginActivity.this, "NAME", nickname);
                                SharePreferenceUtils.putString(LoginActivity.this, "AVATAR", avatarurl);
                                UserManager.getInstance().setLogin(true);
                                ToastUtil.show(LoginActivity.this,"登录成功");
                                finish();
                            } else {
                                SharePreferenceUtils.removeString(LoginActivity.this, "KEY_TOKEN");
                                UserManager.getInstance().setLogin(false);
                                ToastUtil.show(LoginActivity.this,jsonObject.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void requestFailure(int code, String msg) {
                        Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_LONG).show();
                    }
                });
                break;
            case R.id.getTextView:
                getCode();
                break;
        }
    }

    private void getCode() {
        String mobile = usernameEditText.getText().toString();
        if (!TextUtil.isMobile(mobile)) {
            ToastUtil.show(this, "手机号码格式不正确！");
            return;
        }

        getTextView.setEnabled(false);
        getTextView.setText("获取中...");

        JSONObject param = new JSONObject();
        try {
            param.put("number", mobile);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiOkHttp.sendMsgCode(this, param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                try {
                    String msg = data.getString("msg");
                    ToastUtil.show(LoginActivity.this, msg);
                    //倒计时
                    new BaseCountTime(60 * 1000, 1000L) {

                        int totalTime = 60;

                        @Override
                        public void onTick(long millis) {
                            super.onTick(millis);
                            String temp = totalTime-- + "s";
                            getTextView.setText(temp);
                        }

                        @Override
                        public void onFinish() {
                            super.onFinish();
                            getTextView.setEnabled(true);
                            getTextView.setText("获取验证码");
                        }

                    }.start();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void requestFailure(int code, String msg) {
                getTextView.setEnabled(true);
                getTextView.setText("获取验证码");
                ToastUtil.show(LoginActivity.this,msg);
            }
        });
    }


}
